<?php
/**
 * Handles running cron jobs on the Concerto system
 *
 * @package Concerto
 * @subpackage System
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\system;

class Cron {
	
	/**
	 * Available cron intervals for modules to tap into
	 *
	 * @var array
	 */
	public static $intervals = array(
		'5_min'=>300,
		'30_min'=>1800,
		'1_hour'=>3600,
		'6_hour'=>21600,
		'12_hour'=>43200,
		'1_day'=>86400
	);
	
	/**
	 * Check all cron intervals and run them if their time has passed
	 */
	public static function check() {
		$now = time();
		foreach(self::$intervals as $name=>$interval) {
			if(($now - getOption('cron_'.$name)) > $interval) {
				self::run($name);
			}
		}
	}
	
	/**
	 * Run a cron job for a spsecific interval
	 *
	 * @param string $name The interval
	 */
	protected static function run($name) {
		global $concerto;
		$concerto->Modulator->runFunction('cron_'.$name);
		setOption('cron_'.$name, time());	
	}
}

?>